import threading
import time

# CPU 密集型任务
def cpu_intensive_task():
    total = 0
    for i in range(10**7):
        total += i
    print(f"Total: {total}")

# 创建多个线程
threads = []
start_time = time.time()

for _ in range(4):  # 创建 4 个线程
    thread = threading.Thread(target=cpu_intensive_task)
    threads.append(thread)
    thread.start()

# 等待所有线程完成
for thread in threads:
    thread.join()

end_time = time.time()
print(f"Time taken with threads: {end_time - start_time:.2f} seconds")